package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TouchEvent;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.ui.Multitouch;
	import flash.ui.MultitouchInputMode;

	[SWF(width=320, height=460, frameRate=24, backgroundColor=0xEB7F00)] public class MultiTouch extends Sprite
	{
		public static var points:Array = [];

		/*
		   private var dots:Object;
		   private var labels:Object;
		 */

		public function MultiTouch()
		{
			//super();

			/*
			   CONFIG::debugging{
			   this.updateDotsLeft();
			   this.dots = new Object();
			   this.labels = new Object();
			   }
			 */


			if(stage)init(null);
			else addEventListener(Event.ADDED_TO_STAGE,init);
		}
		private function init(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE,init);

			Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
			this.stage.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);
			this.stage.addEventListener(TouchEvent.TOUCH_MOVE, onTouchMove);
			this.stage.addEventListener(TouchEvent.TOUCH_END, onTouchEnd);
		}

		private function onTouchBegin(e:TouchEvent):void
		{
			logs.adds(e.touchPointID,"("+e.stageX,e.stageY+") down");
			if (points.length == Multitouch.maxTouchPoints){
				logs.adds("over flow",points.length);
				this.updateDotsLeft();
				return;
			}
			var o:Object = getItemAtArrById(e.touchPointID);
			if(o){
				o.x = e.stageX;
				o.y = e.stageY;
			}else{
				points.push({id:e.touchPointID,x:e.stageX,y:e.stageY});
			}

			//points.push({id:e.touchPointID,x:e.stageX,y:e.stageY});

			/*

			   CONFIG::debugging{
			//logs.adds(e);
			var dot:Sprite = this.getCircle();
			dot.x = e.stageX;
			dot.y = e.stageY;
			this.stage.addChild(dot);
			dot.startTouchDrag(e.touchPointID, true);
			this.dots[e.touchPointID] = dot;

			var label:TextField = this.getLabel(e.stageX + ", " + e.stageY);
			label.x = 3;
			label.y = points.length * 15;
			this.stage.addChild(label);
			this.labels[e.touchPointID] = label;

			this.updateDotsLeft();
			}
			 */

			//dispatchEvent(e);
			Main.main.downPoint(e.stageX,e.stageY);
		}

		private function getItemAtArrById(id:int):Object
		{
			for each(var o:Object in points)
			{
				if(o.id==id)
					return o;
			}
			return null;
		}

		private function removeItemAtArrById(id:int):Object
		{
			var i:int = 0;
			for each(var o:Object in points)
			{
				if(o.id==id)
				{
					//points.removeAt(i);
					points.splice(i,1);
					logs.adds(i,"removed");
					return o;
				}
				++i;
			}
			return null;
		}

		private function onTouchMove(e:TouchEvent):void
		{
			/*
			   CONFIG::debugging{
			   var label:TextField = this.labels[e.touchPointID];
			   label.text = (e.stageX + ", " + e.stageY);
			   }
			 */

			var o:Object = getItemAtArrById(e.touchPointID);
			if(o){
				o.x = e.stageX;
				o.y = e.stageY;
			}
			Main.main.movePoint(o);
		}

		private function onTouchEnd(e:TouchEvent):void
		{
			var id:int = e.touchPointID;
			removeItemAtArrById(id);

			/*
			   CONFIG::debugging{
			   var dot:Sprite = this.dots[id];
			   var label:TextField = this.labels[id];

			   this.stage.removeChild(dot);
			   this.stage.removeChild(label);

			   delete this.dots[id];
			   delete this.labels[id];

			   }
			 */
			this.updateDotsLeft();
		}

		/*
		   private function getCircle(circumference:uint = 40):Sprite
		   {
		   var circle:Sprite = new Sprite();
		   circle.graphics.beginFill(0x1695A3);
		   circle.graphics.drawCircle(0, 0, circumference);
		   return circle;
		   }

		   private function getLabel(initialText:String):TextField
		   {
		   var label:TextField = new TextField();
		   label.selectable = false;
		   label.antiAliasType = AntiAliasType.ADVANCED;
		   label.text = initialText;
		   return label;
		   }
		 */

		private function updateDotsLeft():void
		{
			logs.adds("numPoints left: ",Multitouch.maxTouchPoints - points.length);
			for each ( var o:Object in points)
			{
				logs.adds(o.id,"("+int(o.x),int(o.y)+")");
			}
			logs.adds("-------------------");

		}
	}
}
